#!/usr/bin/env python
"""
Display the mesh in 3D using the Visual ToolKit

Basile Clout, September 2007
"""

import vtk

# Polydata for the nodes

math = vtk.vtkMath()
points = vtk.vtkPoints()
points.SetDataTypeToFloat()

indices = vtk.vtkIdTypeArray()          # Array containing the colors (index = Cell, value = colorIndex (in the Color Lookup Table))
CellArray = vtk.vtkCellArray() #CellArray containing the points (Not really needed in fact)

for i in range(0,50):
    ptIdx = points.InsertPoint(i, math.Random(0,1), math.Random(0,1), math.Random(0,1))
    #InsertNextPoint((0.0, 3.4, 6.2))
    if i>25:
        indices.InsertNextValue(2)
    else:
        indices.InsertNextValue(3)
    CellArray.InsertNextCell(1)
    CellArray.InsertCellPoint(i)
    
profile = vtk.vtkPolyData()
profile.SetPoints(points)
profile.GetPointData().SetScalars(indices)
#profile.SetVerts(CellArray)

mapMesh = vtk.vtkPolyDataMapper()
mapMesh.SetInput(profile)
meshActor = vtk.vtkActor()
meshActor.SetMapper(mapMesh)


sphere = vtk.vtkSphereSource()
sphere.SetThetaResolution(16)
sphere.SetPhiResolution(32)
sphere.SetRadius(0.025)

spheres = vtk.vtkGlyph3D()
spheres.SetInput(profile)
spheres.SetSource(0, sphere.GetOutput())
spheres.SetSource(1, sphere.GetOutput())
spheres.SetSource(2, sphere.GetOutput())
spheres.SetSource(3, sphere.GetOutput())
spheres.SetColorModeToColorByScalar()
spheres.SetScaleModeToDataScalingOff()
spheres.SetIndexModeToScalar()
spheres.SetRange(0, 3)



# Poly data for the edges
edgeColors = vtk.vtkIdTypeArray()       # Array containing the colors
edgeArray = vtk.vtkCellArray()      # Cell Array containing 2-dimensions cells, that is lines (or edges)
edgeRadii = vtk.vtkFloatArray()         # Radius of the tube representing the edge
edgePoints = vtk.vtkPoints()            # Need these points, because it is necessary to build the Cells
edgePoints.SetDataTypeToFloat()


for i in range(0,10):
    pt1 = edgePoints.InsertNextPoint(math.Random(0,1), math.Random(0,1), math.Random(0,1))
    pt2 = edgePoints.InsertNextPoint(math.Random(0,1), math.Random(0,1), math.Random(0,1))
    
    edgeColors.InsertNextValue(2)       # All the edges have the color "2" from the lut
    edgeArray.InsertNextCell(2)         # The next cell will be dimension 2 (2 points)
    
    edgeArray.InsertCellPoint(pt1)
    edgeArray.InsertCellPoint(pt2)
    
    edgeRadii.InsertNextValue(2.0)
    
edgesPolyData = vtk.vtkPolyData()
edgesPolyData.SetPoints(edgePoints)
edgesPolyData.SetLines(edgeArray)
edgesPolyData.GetCellData().SetScalars(edgeRadii)
edgesPolyData.GetCellData().AddArray(edgeColors)

tubes = vtk.vtkTubeFilter()
tubes.SetInput(edgesPolyData)
tubes.SetVaryRadius(0)
tubes.SetRadius(0.005)
tubes.CappingOff()
tubes.SetNumberOfSides(12)
tubes.SetGenerateTCoordsToOff()





lut = vtk.vtkLookupTable()
lut.SetNumberOfColors(4)
lut.Build()
lut.SetTableValue(0, 0.0, 0.0, 1.0, 1.0)
lut.SetTableValue(1, 0.0, 1.0, 1.0, 1.0)
lut.SetTableValue(2, 1.0, 0.0, 0.0, 1.0)
lut.SetTableValue(3, 1.0, 1.0, 0.0, 1.0)
print lut.GetNumberOfColors()

mapSpheres = vtk.vtkPolyDataMapper()
mapSpheres.SetInputConnection(spheres.GetOutputPort())
mapSpheres.SetLookupTable(lut)
mapSpheres.SetScalarRange(0,3)

mapEdges = vtk.vtkPolyDataMapper()
mapEdges.SetInputConnection(tubes.GetOutputPort())
mapEdges.SetLookupTable(lut)
mapEdges.SetScalarRange(0,3)

#Text
text = vtk.vtkTextActor()
text.SetInput("My text, mymy!")
tprops = text.GetTextProperty()
tprops.SetFontFamilyToArial()
tprops.BoldOff()
tprops.ShadowOff()
tprops.SetLineSpacing(1.0)
tprops.SetFontSize(12)
tprops.SetColor(1.0, 0.0, 0.0)
tprops.SetShadowOffset(2,2)
text.SetDisplayPosition(50,50)

text2 = vtk.vtkVectorText()
text2.SetText("Hihihihiiiii")
extrude = vtk.vtkLinearExtrusionFilter()
extrude.SetInputConnection(text2.GetOutputPort())
extrude.SetExtrusionTypeToNormalExtrusion()
extrude.SetVector(0,0,0.2)
extrude.SetScaleFactor(0.5)
mapText2 = vtk.vtkPolyDataMapper()
mapText2.SetInputConnection(extrude.GetOutputPort())
#text2Actor = vtk.vtkActor()
text2Actor = vtk.vtkFollower()
text2Actor.SetMapper(mapText2)
text2Actor.SetScale(0.1, 0.1, 0.1)
text2Actor.AddPosition(0,0,0)

sphereActor = vtk.vtkActor()
sphereActor.SetMapper(mapSpheres)
sphereActor.GetProperty().SetSpecularColor(0,1,0)
#sphereActor.GetProperty).SetDiffuse(0.8)
#sphereActor.GetProperty().SetAmbient(0.2)

edgeActor = vtk.vtkActor()
edgeActor.SetMapper(mapEdges)
edgeActor.GetProperty().SetOpacity(1.0)
edgeActor.GetProperty().SetInterpolationToGouraud()

#sphereMapper = vtk.vtkPolyDataMapper()
#sphereMapper.SetInputConnection(sphere.GetOutputPort())

#sphereActor = vtk.vtkActor()
#sphereActor.SetMapper(sphereMapper)

#sphereActor2 = vtk.vtkActor()
#sphereActor2.SetMapper(sphereMapper)
#sphereActor2.SetPosition(2,2,2)

ren1 = vtk.vtkRenderer()
ren1.AddActor(sphereActor)
ren1.AddActor(edgeActor)
ren1.AddActor2D(text)
ren1.AddActor(text2Actor)
ren1.SetBackground(0.1, 0.2, 0.4)

ren1.ResetCameraClippingRange()
text2Actor.SetCamera(ren1.GetActiveCamera())

renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren1)
renWin.SetSize(600,600)

iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

style = vtk.vtkInteractorStyleTrackballCamera()
iren.SetInteractorStyle(style)

iren.Initialize()
iren.Start()
